<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1,user-scalable=no">
    <!--<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>-->
    <script src="vue.min.js"></script>
    <link rel="stylesheet" type="text/css" href="bootstrap-3.3.7-dist/css/bootstrap.css">
<body>


<div id="app">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h4 class="panel-tittle">添加品牌</h4>
        </div>
        <div class="panel-body form-inline">
            <label>id：<input type="text" class="form-control" v-model="id"></label>
            <label>name：<input type="text" class="form-control" v-model="name" @keyup.enter="add"></label>

            <!--在vue中，使用事件绑定机制，为元素指定处理函数的时候，如果加了小括号，就可以传参数-->
            <input type="button" value="添加" class="btn btn-primary" @click="add()">

            <label>搜索名称关键字：<input type="text" class="form-control" v-model="keywords"></label>
        </div>
    </div>


    <table class="table table-bordered table-hover table-striped">
        <thead>
        <tr>
            <th>ID</th>
            <th>name</th>
            <th>Ctime</th>
            <th>Operiton</th>
        </tr>
        </thead>

        <tbody>
        <tr v-for="item in search(keywords)" :key="item.id">
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.ctime | dateFormat('')}}</td>
            <td>
                <button @click="del(item.id)">删除</button>
            </td>
        </tr>
        </tbody>

    </table>
</div>

<div id="app2">
    <h2>{{ dt | dateFormat('')}}</h2>
</div>


<script>
    // 定义全局过滤器,对时间进行格式化
    Vue.filter('dateFormat', function (dateStr, pattern) {
        // 根据给定的时间字符串，得到特定的时间
        var dt = new Date(dateStr);

        // 时间格式yyy-mmm-ddd(年月日)
        var y = dt.getFullYear();
        var m = (dt.getMonth() + 1).toString().padStart(2, '0');
        var d = (dt.getDate()).toString().padStart(2, '0');

        if (pattern.toLowerCase() === 'yyy-mmm-ddd') {
            // return y + '-' + m + '-' + d;
            return `${y}-${m}-${d}`;
        } else {
            var hh = dt.getHours().toString().padStart(2, '0');
            var mm = dt.getMinutes().toString().padStart(2, '0');
            var ss = dt.getSeconds().toString().padStart(2, '0');
            return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
        }
    });

    let app = new Vue({
        el: '#app',
        data: {
            id: '',
            name: '',
            keywords: '',
            list: [
                {id: 1, name: '奔驰', ctime: new Date()},
                {id: 2, name: '宝马', ctime: new Date()},
                {id: 3, name: '架飞机', ctime: new Date()},
                {id: 4, name: '咖啡', ctime: new Date()},
                {id: 5, name: '任务', ctime: new Date()},
                {id: 6, name: '啊肯定啦', ctime: new Date()},

            ]
        },
        methods: {
            add() {
                var car = {id: this.id, name: this.name, ctime: new Date()};
                this.list.push(car);
                this.id = this.name = ''
            },
            del(id) { //根据id来删除数据

                // 第一种删除的方法
                // this.list.some((item,i)=>{
                //   if(item.id==id){
                //       this.list.splice(i,1);
                //       return true
                //
                //   }
                // })

                // 第二种删除的方法
                var index = this.list.findIndex(item => {
                    if (item.id == id) {
                        return true;
                    }
                });
                this.list.splice(index, 1);

            },
            search(keywords) {//根据关键字搜索数据
                var newList = [];
                this.list.forEach(item => {
                    if (item.name.indexOf(keywords) != -1) {
                        newList.push(item)
                    }
                });
                return newList
            }
        }
    });

    let app2 = new Vue({
        el: '#app2',
        data: {
            dt: new Date()
        },
        methods: {},
        filters: { //私有的过滤器，，，，计算机优先调用私有过滤器
            dateFormat: function (dateStr, pattern = '') {
                // 根据给定的时间字符串，得到特定的时间
                var dt = new Date(dateStr);

                // 时间格式yyy-mmm-ddd(年月日)
                var y = dt.getFullYear();
                var m = (dt.getMonth() + 1).toString().padStart(2, '0');
                var d = (dt.getDate()).toString().padStart(2, '0');

                if (pattern.toLowerCase() === 'yyy-mmm-ddd') {
                    // return y + '-' + m + '-' + d;
                    return `${y}-${m}-${d}`;
                } else {
                    var hh = dt.getHours().toString().padStart(2, '0');
                    var mm = dt.getMinutes().toString().padStart(2, '0');
                    var ss = dt.getSeconds().toString().padStart(2, '0');
                    return `${y}-${m}-${d} ${hh}:${mm}:${ss}~~~~~~~~~~~~`;

                }
            }
        }
    })


</script>
</body>
</html>